/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricshorizontalpanel.h"
#include "configmanager.h"

const QString& LyricsHorizontalPanel::Builder::getPanelName() const {
    static QString name = LyricsHorizontalPanel::tr("Horizontal");
    return name;
};

LyricsPanel* LyricsHorizontalPanel::Builder::create(QWidget* parent) const {
    return new LyricsHorizontalPanel(parent);
};

LyricsTime LyricsHorizontalPanel::parseMouseScroll(const QPoint& vec) {
    int delta = vec.x();
    return m_startTime.elapsed();
};

void LyricsHorizontalPanel::drawFrame(LyricsTime time) {
    if (!m_lyrics || m_lyrics->isEmpty())
        return;
    // find current lyrics
    LyricsData::const_iterator px = m_lyrics->find(time);
    // if nothing found, exit
    if (px == m_lyrics->end())
        return;

    ConfigManager* cfg = ConfigManager::getInstance();

    // determin where should i print the current line of lyrics
    int timeLength = (px + 1 == m_lyrics->end()) ? 0 : (px + 1)->offset - px->offset;
    int printX = width() * cfg->value("Style/HorizontalLyricsFactor", 0.5).toDouble() -
            ((timeLength == 0) ? 0 : getTextWidth(px->lyrics) * (time - px->offset) / timeLength);
    int printY = (height() - m_painter.fontMetrics().height()) / 2 + fontMetrics().ascent();

    // current lyrics changed
    if (m_midLyrics != &(*px)) {
        m_midLyrics = &(*px);
        updateInterval(getTextWidth(px->lyrics), timeLength);
    };

    // find the first line of lyrics i need to print 
    while (printX > 0 && px != m_lyrics->begin()) {
        --px;
        printX -= getTextWidth(px->lyrics);
    };

    // ok, now i print the lyrics line by line
    while (printX < width() && px != m_lyrics->end()) {
        if (&(*px) == m_midLyrics) {
            // print highlighted line
            drawText(m_painter, printX, printY,
                getFadingColor(time - px->offset, timeLength),
                px->lyrics);
        } else {
            drawText(m_painter, printX, printY,
                cfg->value("Style/FgColor", Qt::darkGreen).value<QColor>(),
                px->lyrics);
        };
        printX += getTextWidth(px->lyrics);
        ++px;
    };
    //m_painter.drawLine(width() / 2, 0, width() / 2, height());
    //m_painter.drawText(0, m_painter.fontMetrics().height() + m_painter.fontMetrics().ascent(), QString::number(time / 60000) + ":" + QString::number(time % 60000 / 1000));
    //m_painter.drawText(0, m_painter.fontMetrics().ascent(), QString("FPS = ") + QString::number(1000.0 / newInterval));
};

int LyricsHorizontalPanel::getTextWidth(const QString& str) {
    ConfigManager* cfg = ConfigManager::getInstance();
    int spacing = cfg->value("Style/HorizontalLyricsSpacing", 10).toInt();
    return fontMetrics().width(str) + spacing;
};
